//
//  HotWordViewController.m
//  suRead
//
//  Created by 师爱华 on 17/3/2.
//  Copyright © 2017年 bao. All rights reserved.
//

#import "HotWordViewController.h"
#import "Masonry.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"
#import "AppDelegate.h"

#import "ServiceUtils.h"
#import "ServiceNews.h"
#import "HotWordService.h"



@interface HotWordViewController () <UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>

/**
 *  点击空白区域事件
 */
@property(nonatomic, strong) UITapGestureRecognizer *resetBottomTapGesture;

@end

@implementation HotWordViewController 

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *linelab = [UILabel new];
    CGRect lineFrame = CGRectMake(0, self.view.bounds.size.height-241, self.view.bounds.size.width,1);
    linelab.frame = lineFrame;
    linelab.backgroundColor = [UIColor colorWithRed:230/255.0f green:230/255.0f blue:160/235.0f alpha:1.0f];
    [self.view addSubview:linelab];
    
    //热门推荐区
    if(self.hotWordTableView == nil){
        CGRect contentViewFrame = CGRectMake(0, self.view.bounds.size.height-240, self.view.bounds.size.width,240);
        self.hotWordTableView = [[UITableView alloc] initWithFrame:contentViewFrame style:UITableViewStylePlain];
        self.hotWordTableView.delegate = self;
        self.hotWordTableView.dataSource = self;
        
        self.hotWordTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //分割线从头开始显示
        self.hotWordTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        [self.view addSubview:self.hotWordTableView];
        
    }
    _resetBottomTapGesture =[[UITapGestureRecognizer alloc]
                             initWithTarget:self
                             action:@selector(tapEmptyArea:)];
    [_resetBottomTapGesture setDelegate:self];
    [self.view addGestureRecognizer:_resetBottomTapGesture];
    
    [self.hotWordTableView registerNib:[UINib nibWithNibName:@"HotWordCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HotWordCell"];
    
    
    UIView *backgroundView = [[UIView alloc] init];
    backgroundView.frame = CGRectMake(0, 0,self.view.bounds.size.width,self.view.bounds.size.height-241);
    backgroundView.backgroundColor = [UIColor colorWithRed:(40/255.0f) green:(40/255.0f) blue:(40/255.0f) alpha:1.0f];
    backgroundView.alpha = 0.4;
    [self.view addSubview:backgroundView];
    
    
    [self loadHotWords];
  
}

//
//    - (void) showMask {
//        CALayer *maskLayer = [CALayer layer];
//        [maskLayer setFrame:CGRectMake(0, 0, 200, 200)];
//        [maskLayer setBackgroundColor:[[UIColor grayColor] CGColor]];
//        [self.view.layer addSublayer:maskLayer];
//
//    }


/**
 *  定义TAB响应方法
 *
 *  @return
 */
- (void)tapEmptyArea:
(UIGestureRecognizer *)gestureRecognizer {
//    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
//    
//        [self.inputBar setInputBarStatus:RCDLiveBottomBarDefaultStatus];
//        self.inputBar.hidden = YES;
//    }
    // 若为UITableViewCellContentView（即点击了tableViewCell），则不截获Touch事件
//    gestureRecognizer.t
//    if ([NSStringFromClass([gestureRecognizer.view class]) isEqualToString:@"UITableViewCellContentView"]) {
//        return ;
//    }
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 输出点击的view的类名
    NSLog(@"%@", NSStringFromClass([touch.view class]));
    
    // 若为UITableViewCellContentView（即点击了tableViewCell），则不截获Touch事件
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return  YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




#pragma mark tableViewDatasouce Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.hotWords count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *wordFrag = [self.hotWords objectAtIndex:indexPath.row];
    HotWordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HotWordCell"];
    cell.hotword = wordFrag;
    [cell setHotWordLabel:wordFrag];
    return cell;
  
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *word = [self.hotWords objectAtIndex:indexPath.row];
    //TODO
    [self.controlDelegate selectedWord:word];
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)loadHotWords
{
    NSString *subURL=@"http://www3.ctdsb.net/index.php?m=live&c=live&a=hotwords";
    
    __weak typeof(self) weak_self = self;
    [ServiceNews doPlayHotWords:subURL success:^(id responseObject, NSString *message) {
        NSArray *words_ = (NSArray *)responseObject;
        weak_self.hotWords = [NSMutableArray array];
        if (words_) {
            [weak_self.hotWords addObjectsFromArray:words_];
        }
        [weak_self.hotWordTableView reloadData];
        [weak_self.hotWordTableView.mj_footer endRefreshing];
    } failure:^(NSString *message) {
        
    }];
}



@end
